// pages/doctor-train-details/doctor-train-details.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    datail: {},
    time: '',
    name: '',
    current: 1,       // 课程tab切换
    courseList: [],   // 课程视频列表
    page: 1,          // 当前页数
    pagesize: 10,     // 每页条数
    id: '',
    isCollect: ''     // 1是2否 收藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.showLoading();
    const that = this;
    const obj = JSON.parse(options.obj);
    console.log(obj)
    const name = obj.classify;
    let param = {};
    param[name] = obj.id;
    this.setData({
      name,
      id: obj.id
    })
    // 获取详情数据
    if ('activityId' == name) {
      this.getDetail('train/activity/getActivityDetail', param)
    } else if ('academicId' == name) {
      this.getDetail('train/academic/getAcademicDetail', param)
    } else if ('courseId' == name) {
      this.getDetail('train/course/getCourseDetail', param)
    } else if ('itemId' == name) {
      this.getDetail('train/item/getBrandItemDetail', param)
    }
  },
  // 获取详情信息
  getDetail(url, param) {
    const that = this;
    app.request({
      url: url,
      data: param,
      success: function(res) {
        const detail = res.data;
        let time = '';
        if (detail.activityDate) {
          time = app.timeData(detail.activityDate)
        }
        if (detail.createTime) {
          time = app.timeData(detail.createTime)
        }
        that.setData({
          detail,
          time,
          isCollect: detail.isCollect
        })
        wx.hideLoading();
        that.videoContext = wx.createVideoContext('myVideo')
      },
      fail: function(res) {
        
      }
    });
  },
  //收藏
  collectTrain(e) {
    const collectType = e.currentTarget.dataset.type;
    const trainId = e.currentTarget.dataset.id;
    const that = this;
    const param = {
      trainId: this.data.id,
      collectType
    };
    app.request({
      url: "doctor/doctor/collectTrain",
      data: param,
      success: function(res) {
        if (res.data==200){
          that.setData({
            isCollect: 1
          })
        }
        if (res.data == 201){
          that.setData({
            isCollect: 2
          })
        }
        
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 1500
        })
      },
      fail: function(res) {
        
      }
    });
  },
  // 课程计划
  coursePlain() {
    const that = this;
    const param = {
      courseId: this.data.id,
      page: this.data.page,
      pagesize: this.data.pagesize
    };
    app.request({
      url: "train/video/getCourseVideoList",
      data: param,
      success: function(res) {
        let data = res.data
        for (let i = 0; i < data.length; i++) {
          data[i].createTime = app.timeData(data[i].createTime)
        }
        that.setData({
          courseList: data
        })
      },
      fail: function(res) {
       
      }
    });
  },
  tabCurrent(e) {
    const current = e.currentTarget.dataset.index;
    current == '2' && this.coursePlain();
    this.setData({
      current
    })
  },
  // 付费报名
  pay() {
    wx.navigateTo({
      url: '../doctor-course-pay/doctor-course-pay?pirce=' + this.data.detail.coursePrice + '&courseid=' + this.data.id
    });
  
  },
  // 免费学习
  subscribeCourse(){
    const param = {
      courseId: this.data.id
    };
    app.request({
      url: "train/course/subscribeCourse",
      data: param,
      success: function (res) {
       
        wx.showModal({
          title: '提示消息',
          content: res.message,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/doctor-my/doctor-my',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },
  //听课
  entryVideo(e){
    // if (this.data.detail.isBuy == 2 && this.data.detail.isFree == 1){
    //   app.showModal("请您先购买此课程")
    //   return
    // }

    const path = e.currentTarget.dataset.path;
    wx.navigateTo({
      url: "../doctor-train-details-video/doctor-train-details-video?path=" + path 
    });
  },

  // 分享
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      success: function() {
        const param = {
          userId: wx.getStorageSync('userid'),
          type: 3
        };
        app.request({
          url: "doctor/memberTaskApi/addBeauty",
          data: param,
          success: function (res) {
            console.log('分享给美丽币给美丽值', res);
          },
          fail: function (res) {
            // app.requestFail(res);
          }
        });
      }
    }
  }
})